package com.deyinet.bdcms.dao.impl;

import com.deyinet.bdcms.core.dao.impl.BaseDaoImpl;
import com.deyinet.bdcms.dao.IAlarmDao;
import com.deyinet.bdcms.entity.Alarm;
import com.deyinet.bdcms.tools.CheckNull;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/13 0013.
 */
@Repository
public class AlarmDao extends BaseDaoImpl<Alarm> implements IAlarmDao {
    @Override
    public Integer deviceAlarmCount(Alarm alarm) {
        String sql = "SELECT COUNT(id) as count_id from alarm where  process != 1 GROUP BY sn ";
        List li = getSession().createSQLQuery(sql).list();
        return li.size();
    }

    @Override
    public Integer lastAlarm() {
        String sql = "SELECT id from alarm WHERE process=0 ORDER BY createdate DESC LIMIT 1 ";
        List li = getSession().createSQLQuery(sql).list();
        if (li.size() > 0) {
            return (Integer) li.get(0);
        }
        return 0;
    }

    @Override
    public void flushAlarm(String sn) {
        String sql = null;
        if (CheckNull.check(sn)) {
            sql = "UPDATE alarm set process = 1 where sn='" + sn + "'; ";
        } else {
            sql = "UPDATE alarm set process = 1;";
        }
        getSession().createSQLQuery(sql).executeUpdate();

    }

    @Override
    public List<Alarm> findByExampleLike(Alarm example, int pageNo, int pageSize, Timestamp beginTime, Timestamp endTime) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
       /* if (example.getSysuserByUserid()!=null){
            criteria.createAlias("sysuserByUserid","user");
            criteria.add(Restrictions.like("user.name",example.getSysuserByUserid().getName()));}*/

        if (beginTime != null && !"".equals(beginTime)) {
            criteria.add(Restrictions.ge("createdate", beginTime));
        }
        if (endTime != null && !"".equals(endTime)) {
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime() + 86400000)));
        }
        return createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();                 //执行查询结果
    }

    @Override
    public long countByExampleLike(Alarm example, Timestamp beginTime, Timestamp endTime) {
        Criteria criteria = getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
     /*   if (example.getSysuserByUserid()!=null){
            criteria.createAlias("sysuserByUserid","user");
            criteria.add(Restrictions.like("user.name",example.getSysuserByUserid().getName()));
        }*/
        if (beginTime != null && "".equals(beginTime)) {
            criteria.add(Restrictions.ge("createdate", beginTime));
        }
        if (endTime != null && "".equals(endTime)) {
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime() + 86400000)));
        }
        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }

    @Override
    public Map<String, Object> getlastAlarm() {
        Map<String, Object> alarm = null;
        String sql = "SELECT * FROM view_alarm_for_query";
        Session session = getSession();
        SQLQuery se = session.createSQLQuery(sql);
        List list = se.list();
        if (list.size() > 0) {
            Object[] objects = (Object[]) list.get(0);
            alarm = new HashMap<>();
            alarm.put("id", objects[0]);
            alarm.put("sn", objects[1]);
            alarm.put("info", objects[2]);
            alarm.put("createdate", objects[3]);
            alarm.put("devicename", objects[4]);
            alarm.put("deviceinfo", objects[5]);
            alarm.put("waterline", objects[6]);
            alarm.put("lat", objects[7]);
            alarm.put("lng", objects[8]);
        }
//        Object[] objects=(Object[])se.list().get(0);

        return alarm;
    }

}